#!/bin/sh /etc/rc.common
# Copyright (C) 2008-2012 OpenWrt.org

START=90

start_instance() {
	local cfg="$1"

	config_get type "$cfg" TYPE

	case "$type" in
	  edge)
		config_get ipaddr "$cfg" 'ipaddr'
		[ -n "$ipaddr" ] || return 1
		config_get supernode "$cfg" 'supernode'
		config_get port "$cfg" 'port'
		config_get community "$cfg" 'community'
		config_get key "$cfg" 'key'
		config_get_bool route "$cfg" 'route' '0'
		[ "$route" = "1" ] && args='-r'
		service_start /usr/sbin/edge -f $args -a $ipaddr -c $community -k $key -l ${supernode}:${port}
		;;
	  supernode)
		config_get port "$cfg" port
		[ -n "$port" ] || return 1
		service_start /usr/sbin/supernode -l $port
		;;
	esac
}

stop_instance() {
	local cfg="$1"

	config_get type "$cfg" TYPE

	case "$type" in
	  edge)
		service_stop /usr/sbin/edge
		;;
	  supernode)
		service_stop /usr/sbin/supernode
		;;
	esac
}

start() {
	config_load 'n2n'
	config_foreach start_instance 'edge'
	config_foreach start_instance 'supernode'
}

stop() {
	config_load 'n2n'
	config_foreach stop_instance 'edge'
	config_foreach stop_instance 'supernode'
}
